/*
Copyright 2014 Yury Kharlamov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated Inversion-Of-Control container
https://code.google.com/p/gen-ioc/

* * *

Object Manager interface
*/
#pragma once

#include "TypeInfoWrapper.h"
#include "Object.h"

typedef std::vector<IfaceId> IfaceIdList;

struct IObjectManager : public IShutdown
{
	virtual void AddFactory(const Factory& factory, const IfaceId& classId, const IfaceIdList& consumes, const IfaceIdList& emits, bool isSingleton) = 0;
	virtual std::shared_ptr<IObject> CreateObjectByIfaceId(const IfaceId& ifaceId, std::weak_ptr<IEventQueue> subscriber) = 0;
	virtual std::shared_ptr<IObject> CreateObjectByClassId(const IfaceId& classId, std::weak_ptr<IEventQueue> subscriber) = 0;
	virtual void WaitShutdownFinished() = 0;
};

struct IFactory
{
	virtual void Add(IObjectManager& man) = 0;
	virtual ~IFactory(){}
};

template <class T>
std::shared_ptr<T> CreateObject(IObjectManager* man)
{
	return std::dynamic_pointer_cast<T>(man->CreateObjectByIfaceId(ID_OF(T), std::weak_ptr<IEventQueue>()));
}

template <class IFaceId, class ClassId>
std::shared_ptr<IFaceId> CreateObject(IObjectManager* man)
{
	return std::dynamic_pointer_cast<IFaceId>(man->CreateObjectByClassId(ID_OF(ClassId), std::weak_ptr<IEventQueue>()));
}

std::shared_ptr<IObjectManager> CreateObjectManager();
std::shared_ptr<IObjectManager> CreateEmbeddedObjectManager();

